//
//  WelComeViewController.swift
//  Sina
//
//  Created by 王杰 on 15/11/17.
//  Copyright © 2015年 王杰. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelComeViewController: UIViewController {

    override func loadView() {
        view = backImage
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //加载控件
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
    
        self.iconImage.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        })

        
        UIView.animateWithDuration(1.2, delay:0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //闭包内部 强制刷新页面
            //将提前收集到的约束的变化 进行更新
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                        print("OK")
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
                })
        }
    }
    
    
    private func setupUI() {
    
    view.addSubview(iconImage)
    view.addSubview(welcomeLabel)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //加载图片
        iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
    
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
    
    }
    
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImage: UIImageView = UIImageView()
    
    private lazy var welcomeLabel:UILabel = {
    
    let lab = UILabel()
    
        lab.textColor = UIColor.lightGrayColor()
//       lab.text = (UserAccountViewModel().userName ?? "") + "欢迎归来"
        lab.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"

        lab.textAlignment = .Center
        lab.font = UIFont.systemFontOfSize(14)
        lab.alpha = 0
        lab.sizeToFit()
    return lab
    }()
}
